package com.puzzlebazar.client.main.view;

import com.puzzlebazar.client.util.EmailFilter;
import com.puzzlebazar.client.util.LabeledFieldImpl;
import com.puzzlebazar.client.util.Validation;
import com.puzzlebazar.client.util.ValidationCallback;

public class EmailField extends LabeledFieldImpl {


	/**
	 * Creates a labeled field which minimal length is validated
	 * 
	 * @param labelText The text to put in the label
	 * @param minLength The minimum valid length
	 * @param errorMessage The error message to display when the minimum length is not reached
	 */
	public EmailField( String labelText, int minLength, String errorMessage ) {
		super( labelText, minLength, new EmailFilter() );
		initialize( errorMessage );
	}


	/**
	 * Completes the constructor initialization
	 * 
	 * @param errorMessage The error message to display when the minimum length is not reached
	 */
	private void initialize(final String errorMessage) {
		addValidationCallback( new ValidationCallback() {
			@Override
			public String validate(String text) {
				if( !Validation.validateEmail( text ) ) return errorMessage;
				return null;
			}
		});
	}	
	
}
